package com.cskaoyan.config;

import com.cskaoyan.converter.String2UserConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.support.ConfigurableConversionService;

import javax.annotation.PostConstruct;

/**
 * @author stone
 * @date 2022/06/01 10:11
 */
//@Configuration
public class ConverterConfiguration {

    // 外科手术 → 心脏搭桥
    // 取出心脏、搭桥、放回去

    // 取出
    @Autowired
    ConfigurableConversionService conversionService;

    @Autowired
    String2UserConverter string2UserConverter;

    // 配置自定义converter
    @PostConstruct
    public void addConverter() {
        //String2UserConverter converter = new String2UserConverter();
        //conversionService.addConverter(converter);
        conversionService.addConverter(string2UserConverter);
    }

    // 放回去
    @Bean
    @Primary  // 如果某个类型的组件在容器中不止一个的时候，按照类型取出会报错；
              // 如果增加该注解，按照类型取出，会取出该组件
    public ConfigurableConversionService conversionService() {
        return conversionService;
    }


}
